<?php

namespace App\Http\Requests\App\V1\User;

use App\Http\Requests\ApiRequest;
use App\Models\Language;
use App\Rules\ImageFile;
use App\Services\OAuth\UserThirdPartyService;
use Illuminate\Http\UploadedFile;
use Illuminate\Validation\Rule;

/**
 * Class UserUpdateRequest
 *
 * @property UploadedFile|string avatar
 * @property string|null nick_name
 * @property int|null sex
 * @property string|null oauth_type
 * @property int|null oauth_user_id
 *
 * @package App\Http\Requests\App\V1\User
 */
class UserUpdateRequest extends ApiRequest
{
    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        $img_rule = (new ImageFile)
            ->limitSize(2048);

        $rule = [
            'nick_name'   => ['nullable', 'string'],
            'avatar'      => ['nullable', $img_rule],
            'sex'         => ['nullable', 'int'],
            'language_id' => ['nullable', Rule::in(Language::all()->modelKeys())],

            // oauth bind rule
            'oauth_type'    => [
                'nullable', 'required_with:oauth_user_id',
                'string', Rule::in(UserThirdPartyService::VALIDATE_OAUTH_TYPES)
            ],
            'oauth_user_id' => ['nullable', 'required_with:oauth_type', 'int', 'min:1'],
        ];

        return $rule;
    }
}
